package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Chapter;
import cn.sunlelin.educationserver.entity.Course;
import cn.sunlelin.educationserver.service.ChapterService;
import cn.sunlelin.educationserver.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @Autowired
    private CourseService courseService;

    @PostMapping("/user/chapter/createChapter")
    public Map<String, Object> createChapter(@RequestBody Chapter chapter,
                                             @RequestHeader("userId") Integer userId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> tCourse = courseService.selectCourseById(chapter.getCourseId());
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "添加章节失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            chapterService.createChapter(chapter);
            map.put("code", 200);
            map.put("msg", "添加章节成功");
            map.put("dataobject", null);
        }
        return map;
    }

    @PostMapping("/user/chapter/updateChapter")
    public Map<String, Object> updateChapter(@RequestBody Chapter chapter,
                                             @RequestHeader("userId") Integer userId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> tCourse = courseService.selectCourseById(chapter.getCourseId());
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "修改章节失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (chapterService.updateChapter(chapter)) {
                map.put("code", 200);
                map.put("msg", "修改章节成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "修改章节失败，该Id不存在");
                map.put("dataobject", null);
            }
        }
        return map;
    }

    @GetMapping("/chapter/selectChapterById/{chapterId}")
    public Map<String, Object> selectChapterById(@PathVariable Integer chapterId) {
        Chapter chapter = chapterService.selectChapterById(chapterId);
        Map<String, Object> map = new HashMap<>();
        if (chapter != null) {
            map.put("code", 200);
            map.put("msg", "根据Id查询章节成功");
            map.put("dataobject", chapter);
        } else {
            map.put("code", 500);
            map.put("msg", "根据Id查询章节失败，该Id不存在");
            map.put("dataobject", null);
        }
        return map;
    }

    @GetMapping("/chapter/selectChapterByCourseId/{courseId}")
    public Map<String, Object> selectChapterByCourseId(@PathVariable Integer courseId) {
        List<Chapter> chapterList = chapterService.selectChapterByCourseId(courseId);
        Map<String, Object> map = new HashMap<>();
        if (chapterList.size() != 0) {
            map.put("code", 200);
            map.put("msg", "根据课程Id查询章节成功");
            map.put("dataobject", chapterList);
        } else {
            map.put("code", 500);
            map.put("msg", "根据课程Id查询章节失败，该Id不存在");
            map.put("dataobject", null);
        }
        return map;
    }

    @GetMapping("/user/chapter/deleteChapter/{chapterId}")
    public Map<String, Object> deleteChapter(@PathVariable Integer chapterId,
                                             @RequestHeader("userId") Integer userId) {
        Map<String, Object> map = new HashMap<>();
        Chapter tChapterId = chapterService.selectChapterById(chapterId);
        Map<String, Object> tCourse = courseService.selectCourseById(tChapterId.getCourseId());
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "删除章节失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (chapterService.deleteChapter(chapterId)) {
                map.put("code", 200);
                map.put("msg", "删除章节成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "删除章节失败，该Id不存在");
                map.put("dataobject", null);
            }
        }
        return map;
    }
}
